package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.components.If;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;

public class FixedAreaAction extends BaseAction<FixedArea> {
	@Resource
	private FixedAreaService service;

	// 保存定区
	@Action(value = "fixedArea_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String save() {
		// 调用业务层，保存定区
		service.save(model);
		return SUCCESS;
	}

	// 有条件的分页查询
	@Action(value = "fixedArea_pageQuery", results = { @Result(name = "success", type = "json") })
	public String fixedAreaPageQuery() {
		// 构造分页查询条件
		Pageable pageable = new PageRequest(page - 1, rows);
		// 构造查询条件对象
		Specification<FixedArea> specification = new Specification<FixedArea>() {
			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 创建一个集合储存条件查询对象
				List<Predicate> li = new ArrayList<>();
				// 构造条件查询对象
				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p1 = cb.equal(root.get("id").as(String.class), model.getId());
					li.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
					li.add(p2);
				}
				return cb.and(li.toArray(new Predicate[0]));
			}
		};
		// 掉用业务层
		Page<FixedArea> pageData = service.findPageData(specification, pageable);
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}

	// 查询为关联订区的方法
	@Action(value = "fixedArea_noAssociationCustomers", results = { @Result(name = "success", type = "json") })
	public String fixedAreaNoAssociationCustomers() {
		// 使用webClient调用webService接口
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9010/crm_management/services/customerService/noassociationcustomers")
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	// 查询已关联订区的方法
	@Action(value = "fixedArea_hasAssociationCustomers", results = { @Result(name = "success", type = "json") })
	public String fixedAreaHasAssociationCustomers() {
		// 使用webClient调用webService接口
		Collection<? extends Customer> collection = WebClient.create(
				"http://localhost:9010/crm_management/services/customerService/noassociationcustomers/" + model.getId())
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	//属性驱动
	private String[] customerIds;

	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}
	
	//关联客户到订区
	@Action(value = "fixedArea_associationCsutomerToFixedArea", results = {@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html")})
	public String fixedAreaAssociationCsutomerToFixedArea() {
		String customeridStr = StringUtils.join(customerIds, ",");
		// 使用webClient调用webService接口
		WebClient.create(
				"http://localhost:9010/crm_management/services/customerService/associationcustomerstofixedarea?customerIdStr="+customeridStr+"&fixedAreaId="+model.getId())
				.put(null);
		return SUCCESS;
	}
	
	//定区关联快递员
	//属性驱动
	private Integer courierId;
	private Integer takeTimeId;

	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}
	@Action(value = "fixedArea_associationCourierToFixedArea", results = {@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html")})
	public String fixedAreaAssociationCourierToFixedArea(){
		service.associationCourierToFixedArea(model,courierId,takeTimeId);
		return SUCCESS;
	}
}
